package com.jason.quectel0805.activities;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jason.quectel0805.R;
import com.jason.quectel0805.daos.UserDao;
import com.jason.quectel0805.db.MyRoomDatabase;
import com.jason.quectel0805.entities.User;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

public class RoomActivity extends AppCompatActivity {
    String dbName = "quectel_jason";
    private UserDao userDao;
    private static final String TAG = "jasonwan";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        //创建数据库
        MyRoomDatabase roomDatabase = Room.databaseBuilder(this, MyRoomDatabase.class, dbName)
                .build();
        userDao = roomDatabase.getUserDao();

    }

    /**
     * 利用Room保存数据到sqlite数据库
     *
     * @param view
     */
    public void saveData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = new User();
                user.name = "石林玉";
                user.age = 16;
                user.sex = "女";

                User user2 = new User();
                user2.name = "Jason";
                user2.age = 19;
                user2.sex = "男";

                User user3 = new User();
                user3.name = "quectel";
                user3.age = 12;
                user3.sex = "未知";

                List<User> userList = new ArrayList<>();
                for (int i = 0; i < 100; i++) {
                    User element = new User();
                    element.name = "user" + i;
                    element.age = ((int) (Math.random() * 10));
                    element.sex = "未知";
                    userList.add(element);
                }
                //通过不定参数批量保存
                userDao.insertUser(user, user2, user3);
                //通过List批量保存
                userDao.insertUser(userList);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RoomActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
    }

    /**
     * 利用Room查询sqlite数据库里面的数据
     *
     * @param view
     */
    public void readData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<User> userList = userDao.selectAllUser();
                for (User user : userList) {
                    Log.d(TAG, "run: " + user.toString());
                }
            }
        }).start();
    }

    /**
     * 利用Room修改数据
     * @param view
     */
    public void updateData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                userDao.updateUser(13, "quectel");
            }
        }).start();
    }

    /**
     * 利用Room删除数据
     * @param view
     */
    public void deleteData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                userDao.deleteUser("未知");
            }
        }).start();
    }
}